package com.weilele.leisure.video_factory.ui.module.local_video


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.FragmentContainerActivity
import com.weilele.leisure.time.R
import com.weilele.leisure.time.databinding.FragmentLocalVideoBinding
import com.weilele.leisure.video_factory.ui.module.common.dialog.PermissionTipsDialog
import com.weilele.leisure.video_factory.ui.module.common.holder.LocalVideoItemHolder
import com.weilele.leisure.video_factory.utils.MediaVideoInfo
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.base.helper.createViewModel
import com.weilele.mvvm.base.livedata.LiveDataWrap

class LocalVideoFragment : BaseBindingFragment<FragmentLocalVideoBinding>() {
    companion object {
        fun start(activity: AppCompatActivity?) {
            activity ?: return
            //            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
//                LocalVideoFragment.start(this)
//            } else {
            PermissionTipsDialog.show(
                activity, mutableListOf(
                    PermissionTipsDialog.Bean(
                        activity.getString(R.string.tips_read_external_storage),
                        mutableListOf(
                            android.Manifest.permission.READ_EXTERNAL_STORAGE,
                            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                        )
                    ),
                )
            ) {
                FragmentContainerActivity.start<LocalVideoFragment>(activity)
            }
//            }
        }
    }

    private val localVideoViewModel by lazy { createViewModel<LocalVideoViewModel>() }

    private val videoAdapter = LocalVideoItemHolder.toAdapter()

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        setTitle(R.string.local_video)
        mBinding.rcv.adapter = videoAdapter
    }

    override fun initData() {
        super.initData()
        localVideoViewModel.getLocalVideo()
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    override fun isNeedToolbar(): Boolean {
        return true
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            localVideoViewModel.videoList.wrapSwitchView {
                onVideoList(it.data)
            }
        )
    }

    /**
     * 获取到本地视频回调
     */
    private fun onVideoList(data: MutableList<MediaVideoInfo>?) {
        videoAdapter.refreshList(data)
    }
}